﻿'    Copyright (C) 2009-2010  Ryan Skeldon <psykad@gmail.com>
'
'    This program is free software; you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation; either version 2 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program; if not, write to the Free Software
'    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Option Explicit On
Imports System.Threading
Imports System.Reflection

Public Class Harness
    Private MyServer As Blitz_Server.Core

    Private _hostAddress As String = "127.0.0.1"
    Private _hostPort As Integer = 12345
    Private _bufferSize As Integer = 8192
    Private _welcome As String = "Welcome!"

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        SysTray.Icon = Blitz_Server.My.Resources.server_disabled
        SysTray.Text = Me.Text
        SysTray.ContextMenuStrip = TrayMenu
        SysTray.Visible = True
        Me.Icon = Blitz_Server.My.Resources.server_default
    End Sub

#Region "Event Handlers"
    ''' <summary>
    ''' Handles the ConnectionsChanged event from the server.
    ''' </summary>
    ''' <param name="NumConnections">Current client count on the server.</param>
    ''' <remarks></remarks>
    Private Sub Server_ConnectionsChanged(ByVal NumConnections As Integer)
        Me.Text = "Clients: " & NumConnections.ToString
        SysTray.Text = "Clients: " & NumConnections.ToString
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Message"></param>
    ''' <remarks></remarks>
    Private Sub Server_ServerMessage(ByVal Message As String)
        txtConsole.AppendText(Message & vbCrLf)
        txtConsole.SelectionStart = txtConsole.Text.Length
    End Sub

    ''' <summary>
    ''' Handles the FormClosing event for the form.
    ''' </summary>
    ''' <remarks>Method checks to see if a server is running and attempts to close it.</remarks>
    Public Sub Handles_ServerMainClosing() Handles Me.FormClosing
        If MyServer IsNot Nothing Then
            MyServer.StopServer()
        End If
    End Sub

    ''' <summary>
    ''' Handles the Resize event for the form.
    ''' </summary>
    ''' <remarks>Method checks the state of the form.
    ''' If the form in minimize, it hides it from the
    ''' taskbar and activates the system tray icon.</remarks>
    Public Sub Handles_ServerMainMinimize() Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Visible = False
            SysTray.Visible = True
        End If
    End Sub
#End Region

#Region "Server Control Methods"
    ''' <summary>
    ''' Event handler for btnServerControl.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Primary component that controls the starting and stopping of the server.</remarks>
    Private Sub ServerControl(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnServerControl.Click
        If btnServerControl.Text = "Start" Then
            StartServer()
        Else
            StopServer()
        End If
    End Sub

    ''' <summary>
    ''' Initializes the server object.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub StartServer()
        btnServerControl.Enabled = False
        txtConsole.Clear()
        Try
            MyServer = New Core(Me)
            AddHandler MyServer.OnConnectionsChanged, AddressOf Server_ConnectionsChanged
            AddHandler MyServer.OnServerMessage, AddressOf Server_ServerMessage
            MyServer.HostAddress = System.Net.IPAddress.Parse(_hostAddress)
            MyServer.HostPort = _hostPort
            MyServer.BufferSize = _bufferSize
            MyServer.Welcome = _welcome

            If MyServer.StartServer() Then
                btnServerControl.Text = "Stop"
                StartServerMenuItem.Enabled = False
                StopServerMenuItem.Enabled = True
                SysTray.Icon = Blitz_Server.My.Resources.server_enabled
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        End Try
        btnServerControl.Enabled = True
    End Sub

    ''' <summary>
    ''' Calls the StopServer method from the server object.
    ''' </summary>
    ''' <remarks>Server is stopped and disposed.</remarks>
    Private Sub StopServer()
        btnServerControl.Enabled = False
        Try
            MyServer.StopServer()
            btnServerControl.Text = "Start"
            StartServerMenuItem.Enabled = True
            StopServerMenuItem.Enabled = False
            SysTray.Icon = Blitz_Server.My.Resources.server_disabled
            MyServer = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        End Try
        btnServerControl.Enabled = True
    End Sub
#End Region

#Region "System Tray Menu Methods"
    ''' <summary>
    ''' Handles the Click event for the StartServerMenuItem.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub StartServerMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartServerMenuItem.Click
        StartServer()
    End Sub

    ''' <summary>
    ''' Handles the Click event for the StopServerMenuItem.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub StopServerMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopServerMenuItem.Click
        StopServer()
    End Sub

    ''' <summary>
    ''' Handles the Click event for the ExitMenuItem.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenuItem.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' Handles the DoubleClick event for the SysTray.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SysTray_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SysTray.MouseDoubleClick
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
    End Sub
#End Region
End Class